//  [9/7/2013 OMEGA] created

#include <QTableView>
#include <QHeaderView>
#include "flowlayout.h"

#include "DuelAssEWorkbench.h"

#include "DuelAssetEditor.h"
#include "DuelAssETagsView.h"
#include "DuelAssEArchiveView.h"
#include "DuelAssEGroupView.h"
#include "DuelAssEContentManager.h"
#include "DuelAssEContentView.h"

#define CHECK_PROJECT_STATUS	if(mCurProject == NULL) return RC_PROJECT_INVALID;

DuelAssetEditor::DuelAssetEditor(QWidget *parent, Qt::WindowFlags flags)
	: QMainWindow(parent, flags),
// 	mCurProject(NULL),
// 	mUndoStack(this),
	mbShowAdvancedFilter(true),
	mWorkbench(new DAssEWorkbench(this))
{
	mArchiveView = new DAssEArchiveView(this);
	mGroupView = new DAssEGroupView(this);
	mContentManager = new DAssEContentManager(this);

	ui.setupUi(this);
	ui.archiveWidget->layout()->addWidget(mArchiveView);
	ui.groupWidget->layout()->addWidget(mGroupView);

	connect(mArchiveView, &DAssEArchiveView::removeArchiveEnable, 
		ui.removeArchivesButton, &QToolButton::setEnabled);
	connect(mArchiveView, &DAssEArchiveView::importResourceEnable,
		ui.importResButton, &QToolButton::setEnabled);
	connect(mGroupView, &DAssEGroupView::removeGroupEnable,
		ui.removeGroupButton, &QToolButton::setEnabled);
	

	ui.scrollArea->setWidget(new DAssETagsView(this));

	ui.actionNewProj->setShortcut(QKeySequence::New);
	ui.actionOpenProj->setShortcut(QKeySequence::Open);
	ui.actionSave_Project->setShortcut(QKeySequence::Save);


	connect(ui.advanceOptionButton, &QToolButton::clicked, this, &DuelAssetEditor::toggleAdvancedFilter);
	
	connect(ui.openArchiveButton, SIGNAL(clicked()), mArchiveView, SLOT(openArchive()));
	connect(ui.removeArchivesButton, SIGNAL(clicked()), mArchiveView, SLOT(removeSelectedArchives()));
	connect(ui.importResButton, SIGNAL(clicked()), mArchiveView, SLOT(importResources()));

	connect(ui.addGroupButton, SIGNAL(clicked()), mGroupView, SLOT(addGroup()));
	connect(ui.removeGroupButton, SIGNAL(clicked()), mGroupView, SLOT(removeSelectedGroup()));

	mContentManager->changeViewMode(DAssEContentManager::DAssECVM_FLOW);
	//updateActions();
	//////////////////////////////////////////////////////////////////////////
	// debug
	mWorkbench->openProject("I:\\TestDAProj\\TestDAProj.daproj");
}

DuelAssetEditor::~DuelAssetEditor()
{
	delete mWorkbench;
}


void DuelAssetEditor::toggleAdvancedFilter()
{
	mbShowAdvancedFilter = !mbShowAdvancedFilter;
	ui.advanceOptionWidget->setVisible(mbShowAdvancedFilter);
	if (mbShowAdvancedFilter)
	{
		QIcon icon;
		icon.addFile(QStringLiteral(":/global/Resources/Images/toggle_up.jpg"),
			QSize(), QIcon::Normal, QIcon::Off);
		ui.advanceOptionButton->setIcon(icon);
	}
	else
	{
		QIcon icon;
		icon.addFile(QStringLiteral(":/global/Resources/Images/toggle_down.png"),
			QSize(), QIcon::Normal, QIcon::Off);
		ui.advanceOptionButton->setIcon(icon);
	}
}

void DuelAssetEditor::setContentView( DAssEContentView* view )
{
	ui.resourceContentArea->setWidget(view);
}

void DuelAssetEditor::setLocationTitle( const QString& loc )
{
	ui.locationEdit->setText(loc);
}

void DuelAssetEditor::displayGroup( const QString& grpName )
{
	mContentManager->displayGroup(grpName);
}

void DuelAssetEditor::displayArchive( const QString& archName, const QString& parentGroup )
{
	mContentManager->displayArchive(archName, parentGroup);
}
